/*
 * Created by JFormDesigner on Tue Jul 07 09:18:49 CST 2020
 */

package com.hcy;

import com.entity.User;
import com.jch.Dao.SendsmsDao;
import com.jch.Dao.UserDao;
import com.xlp.SendSms;

import java.awt.*;
import java.awt.event.*;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * @author admin
 */
public class Forget extends JDialog {
    public Forget() {
        initComponents();
    }
    public static long midTime;
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        jl_mobilePhone = new JLabel();
        jtf_mobilePhone = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        jtf_Code = new JTextField();
        label4 = new JLabel();
        jpf_password = new JPasswordField();
        label5 = new JLabel();
        jb_password = new JButton();
        jpf_password2 = new JPasswordField();
        jta_Password = new JTextArea();
        jta_surePassword = new JTextArea();
        jb_Code = new JButton();

        //======== this ========
        setMinimumSize(new Dimension(500, 400));
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new Color(238, 238, 238));
        setTitle("\u5fd8\u8bb0\u5bc6\u7801");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u5fd8\u8bb0\u5bc6\u7801");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.BOLD, 30));
        contentPane.add(label1);
        label1.setBounds(185, 5, 125, label1.getPreferredSize().height);

        //---- jl_mobilePhone ----
        jl_mobilePhone.setText("\u624b\u673a\u53f7\uff1a");
        jl_mobilePhone.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(jl_mobilePhone);
        jl_mobilePhone.setBounds(145, 65, 60, jl_mobilePhone.getPreferredSize().height);
        contentPane.add(jtf_mobilePhone);
        jtf_mobilePhone.setBounds(215, 60, 140, 30);

        //---- label2 ----
        label2.setText("\u8bf7\u8f93\u51651~11\u4f4d\u6570\u7684\u6570\u5b57");
        label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 10));
        contentPane.add(label2);
        label2.setBounds(220, 90, 135, label2.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u9a8c\u8bc1\u7801\uff1a");
        label3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(label3);
        label3.setBounds(145, 120, 65, label3.getPreferredSize().height);
        contentPane.add(jtf_Code);
        jtf_Code.setBounds(215, 115, 90, 30);

        //---- label4 ----
        label4.setText("\u65b0\u5bc6\u7801\uff1a");
        label4.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(label4);
        label4.setBounds(145, 175, 60, 20);
        contentPane.add(jpf_password);
        jpf_password.setBounds(215, 170, 140, 30);

        //---- label5 ----
        label5.setText("\u786e\u5b9a\u65b0\u5bc6\u7801\uff1a");
        label5.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        contentPane.add(label5);
        label5.setBounds(120, 240, 85, label5.getPreferredSize().height);

        //---- jb_password ----
        jb_password.setText("\u786e\u5b9a\u4fee\u6539");
        jb_password.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jb_password.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 12));
        contentPane.add(jb_password);
        jb_password.setBounds(new Rectangle(new Point(195, 310), jb_password.getPreferredSize()));
        contentPane.add(jpf_password2);
        jpf_password2.setBounds(215, 235, 145, 30);

        //---- jta_Password ----
        jta_Password.setOpaque(false);
        jta_Password.setEnabled(false);
        jta_Password.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 10));
        jta_Password.setDisabledTextColor(Color.black);
        contentPane.add(jta_Password);
        jta_Password.setBounds(215, 200, 145, 25);

        //---- jta_surePassword ----
        jta_surePassword.setEnabled(false);
        jta_surePassword.setOpaque(false);
        jta_surePassword.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 10));
        jta_surePassword.setDisabledTextColor(new Color(0, 51, 204));
        contentPane.add(jta_surePassword);
        jta_surePassword.setBounds(215, 265, 125, 25);

        //---- jb_Code ----
        jb_Code.setText("\u53d1\u9001\u9a8c\u8bc1\u7801");
        jb_Code.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
        jb_Code.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        contentPane.add(jb_Code);
        jb_Code.setBounds(330, 115, 135, 30);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents


        //将背景设置为白色
        getContentPane().setBackground(Color.WHITE);

        //设置logo
        ImageIcon logoIcon = new ImageIcon(Forget.class.getResource("小头像.png"));
        setIconImage(logoIcon.getImage());




        jb_password.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if("".equals(jtf_mobilePhone.getText())){
                    JOptionPane.showMessageDialog(Forget.this,"请输入手机号");
                    return;
                }if("".equals(jtf_Code.getText())){
                    JOptionPane.showMessageDialog(Forget.this,"验证码不能为空");
                    return;
                }if("".equals(jpf_password.getText())){
                    JOptionPane.showMessageDialog(Forget.this,"新密码不能为空");
                    return;
                }if("".equals(jpf_password2.getText())){
                    JOptionPane.showMessageDialog(Forget.this,"确定新密码不能为空");
                    return;
                }
                if (jpf_password.getText().equals(jpf_password2.getText())){
                    UserDao userDao=new UserDao();
                    User user=new User();
                    String sql="update user set uPass=?  where uphone=?";
                    int update = userDao.update(sql, jpf_password.getText(),jtf_mobilePhone.getText());
                    System.out.println(update);

                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(Forget.this,"修改成功");

                    dispose();
                }

            }
        });



        //密码输入时的提醒
        jpf_password.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jpf_password.setText("");
                jta_Password.setFont(new Font("微软雅黑",Font.PLAIN,10));
                jta_Password.setForeground(Color.GRAY);
                jta_Password.setText("6-16个字符，区分大小写");
            }
        });

        jpf_password.addFocusListener(new FocusListener() {
            @Override
            public void focusLost(FocusEvent e) {
                boolean psw= Pattern.matches("^[A-za-z0-9]{6,16}$",jpf_password.getText());
                if(psw==false) {
                    if("".equals(jpf_password.getText()) ) {
                        jta_Password.setText("");
                        jta_Password.setEnabled(false);
                    }else {
                        jta_Password.setEnabled(true);
                        jpf_password.getText();
                        jta_Password.setFont(new Font("微软雅黑", Font.PLAIN, 10));
                        jta_Password.setForeground(Color.CYAN);
                        jta_Password.setText( "  密码格式错误×");
                    }
                }else if(psw==true){
                    jta_Password.setEnabled(true);
                    jpf_password.getText();
                    jta_Password.setFont(new Font("微软雅黑",Font.PLAIN,10));
                    jta_Password.setForeground(Color.GREEN);
                    jta_Password.setText("  密码可以使用√");
                }
            }
            @Override
            public void focusGained(FocusEvent e) {

                jta_Password.setText("6-16个字符，区分大小写");
            }


        });

        //确定密码输入时的提醒
        jpf_password2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                jta_surePassword.setText("");
                jta_surePassword.setEnabled(false);

            }
        });
        jpf_password2.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (jpf_password.getText()!="" ){
                    if("".equals(jpf_password2.getText())){
                        jta_surePassword.setText("请先输入密码");
                    }else if(!jpf_password.getText().equals(jpf_password2.getText())){
                        jta_surePassword.setText("两次密码不一致");
                    }else if(jpf_password.getText().equals(jpf_password2.getText())){
                        jta_surePassword.setText("两次密码一致");
                        jta_surePassword.setEnabled(true);
                        jta_surePassword.setForeground(Color.GREEN);
                    }else{
                        jta_surePassword.setText(" 输入错误！");
                    }
                }

            }
        });

        //限制输入的只能为数字
        jtf_mobilePhone.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char keyChar = e.getKeyChar();
                if (keyChar >= '0' && keyChar<= '9'){
                    return;

                }
                Toolkit.getDefaultToolkit().beep();

                e.consume();
            }
        });
        //限制输入长度
        jtf_mobilePhone.setDocument( new PlainDocument(){
            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                String text = jtf_mobilePhone.getText();
                if (text.length() + str.length() > 11){
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(Forget.this,"超出十一位数！");
                    return;

                }

                super.insertString(offs, str, a);
            }
        });
        jb_Code.addActionListener(new ActionListener() {
            String random = (int) ((Math.random() * 9 + 1) * 100000) + "";
            @Override
            public void actionPerformed(ActionEvent e) {
                new SendSms(jtf_mobilePhone.getText(),random);
                SendsmsDao sendsmsDao=new SendsmsDao();
                    sendsmsDao.InsertValues(jtf_mobilePhone.getText(), random);
                Timer timer=new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        sendsmsDao.deleteSendsms(random);
                    }
                },300000,1);
                timer.cancel();
            }

        });
       //验证码点击事件
        jb_Code.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //发送验证码时的线程

                midTime=60;

                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        try {
                            if (midTime>0){
                                midTime --;
                                long ss = midTime%60;
                                jb_Code.setEnabled(false);
                                jb_Code.setText(ss+"秒后重新发送");
                            }else{
                                jb_Code.setEnabled(true);
                                jb_Code.setText("发送验证码");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }


                    }
                },0,1000);

            }
        });

    }



    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel jl_mobilePhone;
    private JTextField jtf_mobilePhone;
    private JLabel label2;
    private JLabel label3;
    private JTextField jtf_Code;
    private JLabel label4;
    private JPasswordField jpf_password;
    private JLabel label5;
    private JButton jb_password;
    private JPasswordField jpf_password2;
    private JTextArea jta_Password;
    private JTextArea jta_surePassword;
    private JButton jb_Code;
    // JFormDesigner - End of variables declaration  //GEN-END:variables


    public static void main(String[] args)
    {
        new Forget();
    }
}
